//import java.rmi.Remote;
//import java.util.ArrayList;
import java.awt.*;
import javax.swing.*;
import wiiremotej.*;
import wiiremotej.event.*;
//import javax.sound.sampled.*;
//import java.io.*;

/**
 * Implements WiiRemoteListener and acts as a general test class. Note that you can ignore the main method pretty much, as it mostly has to do with the graphs and GUIs.
 * At the very end though, there's an example of how to connect to a remote and how to prebuffer audio files.
 * @author Michael Diamond
 * @version 1/05/07
 */

public class WRLImpl_orig extends WiiRemoteAdapter
{
	
    private static boolean accelerometerSource = true; //true = wii remote, false = nunchuk
//    private static boolean lastSource = true;
//    private static boolean lastSourceNunchuk = true;
    
    private static int width = 800;
    private static int heigth = 600;
    
    private WiiRemote remote;
    private static JFrame graphFrame;
    private static JPanel graph;
    private static int t = 0;
    private static int x = 0;
    private static int y = 0;
//    private static int z = 0;
    
    private static int lastX = 0;
    private static int lastY = 0;
//    private static int lastZ = 0;
    
//    private static int tNunchuk = 0;
//    private static int xNunchuk = 0;
//    private static int yNunchuk = 0;
//    private static int zNunchuk = 0;
    
//    private static int lastXNunchuk = 0;
//    private static int lastYNunchuk = 0;
//    private static int lastZNunchuk = 0;
    
    
    public static void main(String args[])
    {
        //basic console logging options...
        WiiRemoteJ.setConsoleLoggingAll();
        //WiiRemoteJ.setConsoleLoggingOff();
        
        try
        {
            graphFrame = new JFrame();
            graphFrame.setTitle("Accelerometer graph: Wii Remote");
            graphFrame.setSize(width, heigth);
            graphFrame.setResizable(false);
            
            t = width-1;
            graph = new JPanel()
            {
                public void paintComponent(Graphics graphics)
                {
                    if (t >= width /*|| accelerometerSource != lastSource*/)
                    {
                        t = 0;
//                        lastSource = accelerometerSource;
                        graphics.clearRect(0, 0, width, heigth);
                        graphics.fillRect(0, 0, width, heigth);
                        graphics.setColor(Color.WHITE);
                        graphics.drawLine(0, heigth/2, width, heigth/2);
                        
                    }
                    
                    graphics.setColor(Color.RED);
                    graphics.drawLine(t, lastX, t, x);
                    graphics.setColor(Color.GREEN);
                    graphics.drawLine(t, lastY, t, y);
                }
            };
            graphFrame.add(graph);
            graphFrame.setVisible(true);
            graphFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            //Find and connect to a Wii Remote
            WiiRemote remote = null;
//        	WiiRemote remote2 = null;
            
            while (remote == null) {
                try {
                    remote = WiiRemoteJ.findRemote();
                }
                catch(Exception e) {
                    remote = null;
                    e.printStackTrace();
                    System.out.println("Failed to connect remote. Trying again.");
                }
            }
            
        	remote.addWiiRemoteListener(new WRLImpl_orig(remote));
        	remote.setAccelerometerEnabled(true);
//        	remote.setSpeakerEnabled(true);
//        	remote.setIRSensorEnabled(true, WRIREvent.BASIC);
        	remote.setLEDIlluminated(0, true);
            	
/*        	while (remote2 == null) {
                try {
                    remote2 = WiiRemoteJ.findRemote();
                }
                catch(Exception e) {
                    remote2 = null;
                    e.printStackTrace();
                    System.out.println("Failed to connect remote. Trying again.");
                }
            }

            remote2.addWiiRemoteListener(new WRLImpl(remote2));
            remote2.setAccelerometerEnabled(true);
            remote2.setSpeakerEnabled(true);
            remote2.setIRSensorEnabled(true, WRIREvent.BASIC);
            remote2.setLEDIlluminated(1, true);
*/
        }
        catch(Exception e){e.printStackTrace();}
    }
    
    public WRLImpl_orig(WiiRemote remote)
    {
        this.remote = remote;
    }
    
    public void disconnected()
    {
        System.out.println("Remote disconnected... Please Wii again.");
        System.exit(0);
    }
    
    public void statusReported(WRStatusEvent evt)
    {
        System.out.println("Battery level: " + (double)evt.getBatteryLevel()/2+ "%");
    }
    
    public void accelerationInputReceived(WRAccelerationEvent evt)
    {
        if (accelerometerSource)
        {
            lastX = x;
            lastY = y;
//            lastZ = z;
            
//            x = (int)(evt.getXAcceleration()/5*300)+300;
//            y = (int)(evt.getYAcceleration()/5*300)+300;
//            z = (int)(evt.getZAcceleration()/5*300)+300;
            x = (int)(evt.getXAcceleration()*300)+300;
            y = (int)(evt.getYAcceleration()*300)+300;
//            z = (int)(evt.getZAcceleration()*300)+300;
            
            t++;
            
            graph.repaint();
        }
        
        /*System.out.println("---Acceleration Data---");
        System.out.println("X: " + evt.getXAcceleration());
        System.out.println("Y: " + evt.getYAcceleration());
        System.out.println("Z: " + evt.getZAcceleration());
        */
    }
    
    public void extensionInputReceived(WRExtensionEvent evt)
    {
        if (evt instanceof WRNunchukExtensionEvent)
        {
            WRNunchukExtensionEvent NEvt = (WRNunchukExtensionEvent)evt;
            
            if (!accelerometerSource)
            {
                WRAccelerationEvent AEvt = NEvt.getAcceleration();
                lastX = x;
                lastY = y;
//                lastZ = z;
                
//                x = (int)(AEvt.getXAcceleration()/5*300)+300;
//                y = (int)(AEvt.getYAcceleration()/5*300)+300;
//                z = (int)(AEvt.getZAcceleration()/5*300)+300;
                
                x = (int)(AEvt.getXAcceleration()*300)+100;
                y = (int)(AEvt.getYAcceleration()*300)+200;
//                z = (int)(AEvt.getZAcceleration()*300)+100;
                
                t++;
//                lastXNunchuk = xNunchuk;
//                lastYNunchuk = yNunchuk;
//                lastZNunchuk = zNunchuk;
//                
//                xNunchuk = (int)(AEvt.getXAcceleration()/5*300)+300;
//                yNunchuk = (int)(AEvt.getYAcceleration()/5*300)+300;
//                zNunchuk = (int)(AEvt.getZAcceleration()/5*300)+300;
//                
//                tNunchuk++;
                
                graph.repaint();
            }
            
        }
    }
    
    public void extensionConnected(WiiRemoteExtension extension)
    {
        System.out.println("Extension connected!");
        try
        {
            remote.setExtensionEnabled(true);
        }catch(Exception e){e.printStackTrace();}
    }
    
    public void extensionPartiallyInserted()
    {
        System.out.println("Extension partially inserted. Push it in more next time!");
    }
    
    public void extensionUnknown()
    {
        System.out.println("Extension unknown. Did you try to plug in a toaster or something?");
    }
    
    public void extensionDisconnected(WiiRemoteExtension extension)
    {
        System.out.println("Extension disconnected. Why'd you unplug it, eh?");
    }
    
}



/*-Dbluecove.jsr82.psm_minimum_off=true
-Dbluecove.debug=false*/